package edu.gatech.cs2340.group7.wheresmystuffapp.junit;


import java.util.List;

import edu.gatech.cs2340.group7.wheresmystuffapp.R;
import edu.gatech.cs2340.group7.wheresmystuffapp.model.Account;
import edu.gatech.cs2340.group7.wheresmystuffapp.model.DatabaseHelper;
import edu.gatech.cs2340.group7.wheresmystuffapp.presenters.CreateAccountPresenter;
import edu.gatech.cs2340.group7.wheresmystuffapp.views.CreateAccountView;
import android.test.ActivityInstrumentationTestCase2;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

@SuppressWarnings("unused")
public class CreateAccountPresenterJUnitTest extends ActivityInstrumentationTestCase2<CreateAccountView>{
	private CreateAccountPresenter myPresenter;
	private CreateAccountView myView;
	private Button bCreateAccount;
	private Button bAlreadyHaveAccount;
	private EditText etEmail;
	private EditText etPassword;
	private EditText etConfirmPassword;
	private EditText etPhoneNumber;
	private TextView tvCreateAccountResult;
	private boolean found;
	
	public CreateAccountPresenterJUnitTest() {
		super(CreateAccountView.class);
		System.out.println("In Constructor 1");
	}
	
	public CreateAccountPresenterJUnitTest(Class<CreateAccountView> activityClass) {
		super(activityClass);
		System.out.println("In Constructor 2");
		// TODO Auto-generated constructor stub
	}
	
	public void setUp() {
		Log.d("setUp", "Starting setUp method");
		setActivityInitialTouchMode(false);
		
		myView = getActivity();
		
		bCreateAccount = (Button) myView.findViewById(R.id.bCreateAccount);
		bAlreadyHaveAccount = (Button) myView.findViewById(R.id.bHaveAccountAlready);
		etEmail = (EditText) myView.findViewById(R.id.etEmail);
		etPassword = (EditText) myView.findViewById(R.id.etPassword);
		etConfirmPassword = (EditText) myView.findViewById(R.id.etConfirmPassword);
		etPhoneNumber = (EditText) myView.findViewById(R.id.etPhoneNumber);
		tvCreateAccountResult = (TextView) myView.findViewById(R.id.tvCreateAccountResult);
	}
	
	public void testKeyEntry() {
		try {
			//Thread thre = new Thread(new MyRunnable());
			runTestOnUiThread(new MyRunnable());
		} catch (Throwable e) {
			Log.d("Catch", "Oops. You caught an exception");
			e.printStackTrace();
		}
	}
	
	private class MyRunnable implements Runnable {
		
		public void run() {
			Log.d("Application", "Beginning Run");
			if(etEmail == null) Log.d("Runnable", "etEmail is null");
			etEmail.requestFocus();
			sendKeys("U S E R A");
			
			Log.d("Application", "Typing password");
			etPassword.requestFocus();
			sendKeys("p a s s w o r d A");
			
			Log.d("Application", "Typing confirm password");
			etConfirmPassword.requestFocus();
			sendKeys("p a s s w o r d A");
			
			Log.d("Application", "Typing phone number");
			etPhoneNumber.requestFocus();
			sendKeys("6 1 5 9 4 8 2 5 4 1");
			
			bCreateAccount.performClick();
			
			Log.d("Application", "Click performed");
			DatabaseHelper myHelp = new DatabaseHelper(myView);
			List<Account> allAccounts = myHelp.getAllAccounts();
			
			found = false;
			for(Account a: allAccounts) {
				Log.d("Application", a.toString());
				if("USERA".equals(a.getEmail())) {
					found = true;
					break;
				}
			}
			assertTrue("Person not added", found);
		}
	}
	
} // close CreateAccountPresenterJUnitTest

